package com.vvvtimes.demo.service;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.XmlUtil;
import com.vvvtimes.demo.common.dto.RestResponse;
import com.vvvtimes.demo.domain.dto.CharlesDTO;
import com.vvvtimes.demo.domain.dto.QueryDnsDTO;
import com.vvvtimes.demo.domain.vo.AliDnsResolveVo;
import com.vvvtimes.demo.domain.vo.NavicateConnectionVo;
import com.vvvtimes.demo.util.NavicatePasswordUtil;
import com.vvvtimes.demo.util.OkHttp3ClientManager;
import com.vvvtimes.demo.util.charles.CharlesKeygen;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;


@Slf4j
@Service
public class SoftwareService {


    public RestResponse<List<NavicateConnectionVo>> navicateConnectionDecode(MultipartFile file) {
        RestResponse<List<NavicateConnectionVo>> result = new RestResponse<>();
        List<NavicateConnectionVo> list = new ArrayList<>();
        String content = null;
        try {
            content = IoUtil.readUtf8(file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(content);
        if(content!=null){
            String xmlData=content;  // 测试时自己替换下
            Document document = XmlUtil.parseXml(xmlData);
            //获得XML文档根节点
            Element elementRoot = XmlUtil.getRootElement(document);
            //打印节点名称
            System.out.println(elementRoot.getTagName());
            //获取下层节点（该方法默认取第一个）
            List<Element> connection = XmlUtil.getElements(elementRoot, "Connection");
            for (Element element:connection) {
                //读取属性ConnectionName
                String connectionName = element.getAttribute("ConnectionName");
                String connType = element.getAttribute("ConnType");
                String host = element.getAttribute("Host");
                String port = element.getAttribute("Port");
                String password = element.getAttribute("Password");
                NavicatePasswordUtil navicatePasswordUtil = new NavicatePasswordUtil(12);
                byte[] decode = navicatePasswordUtil.decrypt(password);
                String decodePassword = null;
                if(decode !=null && decode.length>0){
                    try {
                        decodePassword = new String(decode, "utf-8");
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    System.out.println(decodePassword);
                }
                System.out.println(connectionName+connType+host+port+password);
                NavicateConnectionVo build = NavicateConnectionVo.builder()
                        .connectionName(connectionName)
                        .connType(connType)
                        .host(host)
                        .port(port)
                        .password(decodePassword)
                        .build();
                list.add(build);

            }
        }
        result.setResult(list);

        return result;
    }

    //参考代码1 https://www.zzzmode.com/mytools/charles/main.go
    //参考代码2 https://gitee.com/thom/charles-keygen/tree/master
    //仅学习交流使用
    public RestResponse<String> charlesKeygen(CharlesDTO charlesDTO) {
        RestResponse<String> result = new RestResponse<>();
        String data = charlesDTO.getData();
        String keygen = CharlesKeygen.keygen(data);
        result.setResult(keygen);
        return result;
    }
}
